package com.holyn.guofang.view.my;

import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.holyn.guofang.R;
import com.holyn.guofang.model.sharepreference.UserInfoSharePreference;
import com.holyn.guofang.model.webhttp.HttpConfigure;
import com.holyn.guofang.model.webhttp.wo.DeviceWo;
import com.holyn.guofang.model.webhttp.wo.StatusWo;
import com.holyn.guofang.model.webhttp.wo.UserCardListWo;
import com.holyn.guofang.model.webhttp.wo.UserInfoWo;
import com.holyn.guofang.presenter.BaseView;
import com.holyn.guofang.presenter.my.MyContract;
import com.holyn.guofang.presenter.my.MyPresenter;
import com.holyn.guofang.view.BaseActivity;
import com.holyn.guofang.viewcommon.popupwindow.PopupSingleSelectFromBottom;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Holyn on 2017/8/21.
 */

public class MyChangeDeviceActivity extends BaseActivity implements BaseView, MyContract.ChangeDeviceView{
    private static final int REQUEST_ACTION_POST_DEVICE_LIST = 0x0001;
    private static final int REQUEST_ACTION_POST_MAIN_DEVICE_CODE = 0x0002;
    private static final int REQUEST_ACTION_POST_UPDATE_MAIN_DEVICE = 0x0003;
    private static final int REQUEST_ACTION_POST_USER_INFO = 0x0004;
    private static final int REQUEST_ACTION_POST_USER_CARD_LIST = 0x0005;
    private static final int REQUEST_ACTION_POST_USER_INFO_REFLASH = 0x0006;

    @BindView(R.id.tv_device_name)
    TextView tvDeviceName;
    @BindView(R.id.ll_select_device)
    LinearLayout llSelectDevice;
    @BindView(R.id.tv_secondary_device_name)
    TextView tvSecondaryDeviceName;
    @BindView(R.id.tv_get_valid_code)
    TextView tvGetValidCode;
    @BindView(R.id.et_valid_code)
    EditText etValidCode;
    @BindView(R.id.btn_submit)
    Button btnSubmit;

    MyContract.Presenter presenter;
    private List<DeviceWo.DataBean> deviceList = new ArrayList<>();

    private PopupSingleSelectFromBottom selectDevicePopop = null;
    private List<String> deviceNameList = new ArrayList<>();

    int deviceID = -1;
    String validCode = "";

    @Override
    protected int getLayoutResID() {
        return R.layout.activity_my_change_device;
    }

    @Override
    protected void onMyCreate() {
        ButterKnife.bind(this);
        showToolbar("替换主设备");

        presenter = new MyPresenter(this);
        presenter.addChangeDeviceView(this);
        presenter.postDeviceList(REQUEST_ACTION_POST_DEVICE_LIST, -1);
    }

    @OnClick({R.id.ll_select_device, R.id.tv_get_valid_code, R.id.btn_submit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ll_select_device:
                if (deviceNameList.size() == 0){
                    toastMsg("你没有设备可替换");
                } else {
                    showSelectDevicePopop();
                }
                break;
            case R.id.tv_get_valid_code:
                if (deviceNameList.size() == 0){
                    toastMsg("你没有可授权的替换");
                    return;
                }
                presenter.postMainDeviceCode(REQUEST_ACTION_POST_MAIN_DEVICE_CODE);
                break;
            case R.id.btn_submit:
                if (TextUtils.isEmpty(validCode)){
                    toastMsg("请选择授权的设备");
                    return;
                }
                presenter.postUserInfo(REQUEST_ACTION_POST_USER_INFO);
                break;
        }
    }

    private void showSelectDevicePopop(){
        if (selectDevicePopop == null){
            selectDevicePopop = new PopupSingleSelectFromBottom(this, "选择设备", deviceNameList);
            selectDevicePopop.setOnItemClickListener(new PopupSingleSelectFromBottom.OnItemClickListener() {
                @Override
                public void onItemClick(String item, int position) {
                    tvSecondaryDeviceName.setText(item);
                    deviceID = deviceList.get(position).getId();
                    validCode = item;
                }
            });
        }
        selectDevicePopop.showPopupWindow();
    }

    @Override
    public void requestStart(Integer requestAction) {
        if (requestAction != REQUEST_ACTION_POST_UPDATE_MAIN_DEVICE || requestAction != REQUEST_ACTION_POST_USER_CARD_LIST){
            showLoadingDialog();
        }
    }

    @Override
    public void requestError(Integer requestAction, Throwable e) {
        toastMsg("出现异常");
        if (requestAction != REQUEST_ACTION_POST_UPDATE_MAIN_DEVICE || requestAction != REQUEST_ACTION_POST_USER_CARD_LIST){
            closeLoadingDialog();
        }

    }

    @Override
    public void requestComplete(Integer requestAction) {
        closeLoadingDialog();
    }

    @Override
    public void responseSuccess(Integer requestAction, DeviceWo deviceWo) {
        if (deviceWo.getStatus() == HttpConfigure.STATUS_SUCCESS){
            deviceList.clear();
            deviceNameList.clear();
            deviceList.addAll(deviceWo.getData());

            for (DeviceWo.DataBean data: deviceList){
                String name = data.getRemark();
                if (TextUtils.isEmpty(name)){
                    name = "设备-"+data.getId();
                }
                if (data.isIsMainEquimment()){
                    tvDeviceName.setText("当前设备别名："+name);
                } else {
                    deviceNameList.add(name);
                }
            }

            if (deviceNameList.size() > 0){
                tvSecondaryDeviceName.setText(deviceNameList.get(0));
                btnSubmit.setVisibility(View.VISIBLE);
            }
        } else {
            toastMsg("获取设备列表失败");
        }
    }

    @Override
    public void responseSuccess(Integer requestAction, StatusWo statusWo) {
        if (requestAction == REQUEST_ACTION_POST_MAIN_DEVICE_CODE){
            if (statusWo.getStatus() == HttpConfigure.STATUS_SUCCESS){
                toastMsg("发送验证码成功");
            } else {
                toastMsg("发送验证码失败");
            }
        } else if (requestAction == REQUEST_ACTION_POST_UPDATE_MAIN_DEVICE){
            if (statusWo.getStatus() == HttpConfigure.STATUS_SUCCESS){
                toastMsg("替换主设备成功");
                presenter.postUserInfo(REQUEST_ACTION_POST_USER_INFO_REFLASH);
            } else {
                toastMsg("替换主设备失败");
            }
        }
        closeLoadingDialog();
    }

    @Override
    public void responseSuccess(Integer requestAction, UserInfoWo userInfoWo) {
        if (userInfoWo.getStatus() == HttpConfigure.STATUS_SUCCESS){
            if (requestAction == REQUEST_ACTION_POST_USER_INFO){
                UserInfoWo.UserInfoBean.UserCardBean cardBean = userInfoWo.getUserInfo().getUserCard();
                if (cardBean == null){//代表卡已过期
                    presenter.postUserCardList(REQUEST_ACTION_POST_USER_CARD_LIST, 2, -1, -1);//请求后只做提示
                } else {//未过期
                    presenter.postUpdateMainDevice(REQUEST_ACTION_POST_MAIN_DEVICE_CODE, deviceID, validCode);
                }
            } else if (requestAction == REQUEST_ACTION_POST_USER_INFO_REFLASH){
                UserInfoSharePreference.saveUserInfo(MyChangeDeviceActivity.this, userInfoWo);

                deviceList.clear();
                selectDevicePopop = null;
                deviceNameList.clear();
                deviceID = -1;
                validCode = "";
                presenter.postDeviceList(REQUEST_ACTION_POST_DEVICE_LIST, -1);
            }
        } else {
            closeLoadingDialog();
            toastMsg("获取用户详情失败");
        }
    }

    @Override
    public void responseSuccess(int requestAction, UserCardListWo userCardListWo) {
        if (userCardListWo.getStatus() == HttpConfigure.STATUS_SUCCESS){
            List<UserCardListWo.DataBean> dataBeanList = userCardListWo.getData();
            if (dataBeanList == null){
                toastMsg("当前国方卡已过期，请前往 我的-购国方卡 购卡");
            } else {
                if (dataBeanList.size() == 0){
                    toastMsg("当前国方卡已过期，请前往 我的-购国方卡 购卡");
                } else {
                    toastMsg("当前国方卡已过期，请前往 我的-用卡管理 激活卡");
                }
            }
        } else {
            toastMsg("获取获取用户的国方卡信息失败");
        }
        closeLoadingDialog();
    }
}
